<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui'}){%>
    <style type="text/css">
        body{padding-top:10px;padding-left: 10px}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
        .has-no{color:#3366cc;}
        .has-success{color: #1ab394;}
        .has-warning{color: #f8ac59;}
        .has-error{color: #ed5565;}
        .remark {
            width: 100%; /*自动适应父布局宽度*/
            overflow: auto;
            word-break: break-all;
        }
    </style>
    <%}%>
</head>

<body>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins" style="margin-bottom: 0px;">
                <input type="hidden" readonly id="productMember" name="productMember" class="form-control" value="${user.userRealname}">
                <div class="ibox-content" style="padding:15px 20px 0px 20px;">
                    <form id="search-form" role="form" class="form-horizontal" autocomplete="off">
                        <div class="form-group">
                            <label class="control-label col-sm-1">渠道:</label>
                            <div class="col-sm-2">
                                <select  name="orderChannelName" id="orderChannelName" class="form-control">
                                    <option value="">请选择渠道</option>
                                    <%for(var dict in channel){%>
                                    <option value="${dict.channelName}"  >${dict.channelName}</option>
                                    <%}%>
                                </select>
                            </div>
                            <label class="control-label col-sm-1">SKU:</label>
                            <div class="col-sm-2">
                                <input type="text" name="orderSku" id="orderSku" placeholder="请填写SKU" class="form-control">
                            </div>
                            <label class="control-label col-sm-1">请购单号:</label>
                            <div class="col-sm-2">
                                <input type="text" name="orderNo" id="orderNo" placeholder="请填写请购单号" class="form-control">
                            </div>
                            <label class="control-label col-sm-1">状态:</label>
                            <div class="col-sm-2">
                                <select  name="orderStatus" id="orderStatus" class="form-control">
                                    <option value="">请选择状态</option>
                                    <option value="1"  >未审核</option>
                                    <option value="2"  >审核中</option>
                                    <option value="3"  >审核通过</option>
                                    <option value="4"  >审核未通过</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-sm-1">商品名称:</label>
                            <div class="col-sm-2">
                                <input type="text" name="productName" id="productName" placeholder="请填写商品名称" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-1">类型:</label>
                            <div class="col-sm-2">
                                <select id="timeType" name="timeType" class="form-control">
                                    <option value="">全部</option>
                                    <option value="1">请购时间</option>
                                    <option value="2">审批时间</option>
                                </select>
                            </div>
                            <label class="control-label col-sm-1">开始时间:</label>
                            <div class="col-sm-2">
                                <input type="text" name="beginDate" id="beginDate" placeholder="请选择开始时间" class="form-control">
                            </div>
                            <label class="control-label col-sm-1">结束时间:</label>
                            <div class="col-sm-2">
                                <input type="text" name="endDate" id="endDate" placeholder="请选择结束时间" class="form-control">
                            </div>


                            <a class="search-btn btn btn-success" style="margin-bottom: 0px;">查询</a>
                        </div>
                    </form>
                </div>
            </div>
        <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
            <button  class="add-btn layui-btn layui-btn-normal layui-btn-sm" style="margin-top: -6px;margin-left: -4px" id="import-btn"  name="import-btn">
                <i class="fa fa-plus" aria-hidden="true"></i> 导入请购单
            </button>
            <button  class="export-head-btn layui-btn  layui-btn-sm" style="margin-top: -6px;margin-left: -2px">
                <i class="fa fa-download" aria-hidden="true"></i> 导出表头数据
            </button>
            <button  class="export-btn layui-btn  layui-btn-sm" style="margin-top: -6px;margin-left: -2px">
                <i class="fa fa-download" aria-hidden="true"></i> 导出明细数据
            </button>
            <button  class="layui-btn  layui-btn-sm" style="margin-top: -6px;margin-left: -2px">
                <i class="fa fa-download"></i>
                <a href="/excel/请购单模板.xlsx" style="color: white" download>模板下载</a>
            </button>
        </div>
        <!-- START 右侧数据 -->
        <!-- START 数据表格 -->
        <table id="relatedTable"
               data-toolbar="#toolbar"
               data-side-pagination="server"
               data-pagination="true"
               data-resizable="true"
               data-side-pagination="server"
               data-url="/sea/data/order/findByCnd"
               data-page-size="15"
               class="table table-striped table-hover"
        >
        </table>
        <!-- END 数据表格 -->

        </table>
        <!-- END 右侧数据 -->
    </div>
    </div>
</div>


<div id="layer_window" style="display: none;overflow-x: hidden;overflow-y: hidden">
    <!-- START 内容部分 -->
    <div class="container" >
        <form class="form-horizontal" id="info-form" method="post" autocomplete="off" style="margin: auto">
            <input type="hidden" id="id" name="id">
            <div class="form-group" style="margin-top: 20px">
                <label class="category control-label col-sm-2" style="max-width: 100px">请购数量</label>
                <div class="col-sm-6" style="max-width: 300px">
                    <input type="text" id="orderNumber" name="orderNumber" value="" class="form-control">
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="category control-label col-sm-2" style="max-width: 100px">预计交期</label>
                <div class="col-sm-6" style="max-width: 300px">
                    <input type="text" id="orderDate" name="orderDate" value="" class="form-control">
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-7" style="max-width: 100px"></label>
                <button type="button" class="save-btn btn btn-primary">
                    <i class="fa fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
    <!-- END 内容部分 -->
</div>


<div id="layer_check_window" style="display: none;overflow-x: hidden;overflow-y: hidden">
    <!-- START 内容部分 -->
    <div class="container" >
        <form class="form-horizontal" id="info-form_check" method="post" autocomplete="off" style="margin: auto">
            <input type="hidden" id="order_id" name="id">
            <div class="form-group" style="margin-top: 15px">
                <label class="category control-label col-sm-2" style="max-width: 100px">状态</label>
                <div class="col-sm-6" style="max-width: 300px">
                    <label class="radio-inline" style="padding-top: 9px;">
                        <input type="radio" style="margin-top: 4px;"  name="orderStatus" id="optionsRadios3" value="3" checked> 审核通过
                    </label>
                    <label class="radio-inline" style="padding-top: 9px;">
                        <input type="radio" style="margin-top: 4px;" name="orderStatus" id="optionsRadios4"  value="4"> 审核未通过
                    </label>
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="category control-label col-sm-2" style="max-width: 100px">备注</label>
                <div class="col-sm-6" style="max-width: 400px;">
                    <textarea id="orderRemark" name="orderRemark" class="remark form-control" rows="7"></textarea>
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-7" style="max-width: 100px"></label>
                <button type="button" class="save-check-btn btn btn-primary">
                    <i class="fa fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
    <!-- END 内容部分 -->
</div>

<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer,layui,upload'}){}%>
<script>
    laydate.render({
        elem: '#orderDate',
        type: 'date',
        format: 'yyyy-MM-dd',
        trigger: 'click'
    });
    laydate.render({
        elem: '#beginDate',
        type: 'date',
        format: 'yyyy-MM-dd',
        trigger: 'click'
    });
    laydate.render({
        elem: '#endDate',
        type: 'date',
        format: 'yyyy-MM-dd',
        trigger: 'click'
    });

    var $relatedTable = $('#relatedTable'),selections=[];
    $(function(){
        //START 表格配置
        $relatedTable.bootstrapTable({
            detailView: true,
            columns: [
                {
                    title: '请购单号',
                    field: 'orderNo',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '请购渠道',
                    field: 'orderChannelName',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '请购数量',
                    field: 'orderNumber',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '已请购数量',
                    field: 'buyNumber',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '请购时间',
                    field: 'createTime',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        return v.substr(0,10);
                    }
                },
                {
                    title: '请购人',
                    field: 'orderMember',
                    align: 'center',
                    valign: 'middle'
                },

                {
                    title: '审批人',
                    field: 'orderCheckMember',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '审批时间',
                    field: 'orderCheckTime',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '备注',
                    field: 'orderRemark',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '关联采购单',
                    field: 'produceNo',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r){
                        if(null != v && v.length>=20){
                            return v.substring(0,20)+"...";
                        }else{
                            return v;
                        }
                    }
                },
                {
                    title: '状态',
                    field: 'orderStatus',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        if(v==1){
                            return '<label class="control-label has-no"><i class="fa fa-circle" aria-hidden="true"></i> 未审核</label>';
                        }else if(v==2){
                            return '<label class="control-label has-warning"><i class="fa fa-circle" aria-hidden="true"></i> 审核中</label>';
                        }else if(v==3){
                            return '<label class="control-label has-success"><i class="fa fa-circle" aria-hidden="true"></i> 审核通过</label>';
                        }else if(v==4){
                            return '<label class="control-label has-error"><i class="fa fa-circle" aria-hidden="true"></i> 审核未通过</label>';
                        }
                    }
                }, {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function() {
                //删除数据字典详情信息
                $('.del-btn').on('click',function(){
                    var delid = $(this).attr('data-val'),delname = $(this).attr('data-val-no');
                    //删除提示
                    layer.confirm('确定要删除当前请购单？<br>['+delname+']', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        $.post('/sea/data/order/delete.do',{id: delid},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    $relatedTable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,time: 1500});
                            }
                        });
                    });
                });
                //审核
                $('.pass-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    var orderNo=$(this).attr('data-val-no');
                    layer.confirm('一经提交审核,无法取消,是否提交【'+orderNo+'】？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        layer.closeAll();
                        layer.load();
                        $.post('/sea/data/order/checkOrder.do',{id: id,orderStatus:2},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    $relatedTable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,time: 1500});
                            }
                        });
                        layer.closeAll();
                    });
                });

                //审核
                $('.produce-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    var orderNo=$(this).attr('data-val-no');
                    layer.confirm('一经生成,无法取消,是否提交【'+orderNo+'】？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        layer.closeAll();
                        layer.load();
                        $.post('/sea/data/order/produce.do',{id: id},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    $relatedTable.bootstrapTable('refresh');
                                    layer.closeAll();
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){
                                        layer.closeAll();
                                    }});
                            }
                        });

                    });
                });

                $('.export-produce-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    var orderNo=$(this).attr('data-val-no');
                    layer.confirm('是否要模拟请购单【'+orderNo+'】？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        layer.closeAll();
                        layer.load();
                        window.location.href='/sea/supply/buy/testProduce.do?orderId='+id;
                        layer.closeAll();
                    });
                });

                //新增
                $('.check-btn').on('click',function(){//新增
                    var id = $(this).attr('data-val');
                    $("#order_id").val(id);
                    layer.open({
                        type:1,
                        title:'审批',
                        area:['600px','450px'],
                        skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_check_window')
                    })
                });

                $('.detail-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    layer.open({
                        type: 2,
                        area: ['700px','500px'],
                        content: ['/sea/data/order/detail.do?id='+id, 'yes']
                    });
                });

            },
            onExpandRow: function (index, row, $detail) {
                InitSubTable(index, row, $detail);
            }
        })
        //操作菜单
        function optFormatter(value,row){
            var content = '';
            if(row.orderStatus==1) {
                content += '<button class="pass-btn btn btn-xs btn-success" data-val-no="' + row.orderNo + '" data-val="' + row.id + '" title="提交审核"><i class="fa fa-check-square-o" aria-hidden="true"></i> 提交审核 </button>';
                content += '&nbsp;<button class="del-btn btn btn-xs btn-danger" data-val-no="' + row.orderNo + '" data-val="' + row.id + '" title="删除"><i class="fa fa-trash-o"></i> 删除</button>';
            }
            if(row.orderStatus==2){
                content += '<button class="check-btn btn btn-xs btn-success" data-val-no="' + row.orderNo + '" data-val="' + row.id + '" title="审批"><i class="fa fa-check-square-o" aria-hidden="true"></i> 审批 </button>';
            }
            if((row.produceNo==null || row.produceNo=='') && row.orderStatus==3) {
                content += '<button class="produce-btn btn btn-xs btn-success" data-val-no="' + row.orderNo + '" data-val="' + row.id + '" title="提交审核"><i class="fa fa-check-square-o" aria-hidden="true"></i> 一键生成采购单 </button>';
                content += '<button class="export-produce-btn btn btn-xs btn-primary" data-val-no="' + row.orderNo + '" data-val="' + row.id + '" title="模拟采购单"><i class="fa fa-download"></i> 模拟采购单 </button>';
            }
            content += '&nbsp;<button class="detail-btn btn btn-xs btn-default" data-val="'+row.id+'" title="查看记录"> 查看记录</button>';
            return content;
        }
    });


    var cur_table;
    var orderStatus;
    //初始化子表格(无线循环)
    function InitSubTable (index, row, $detail) {
        var parentId = row.id;
        orderStatus=row.orderStatus;
        cur_table = $detail.html('<table id="child_table" class="table table-striped table-hover"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: '/sea/data/orderDetail/findByOrderId',
            method: 'get',
            clickToSelect: true,
            queryParams: { orderId: parentId },
            ajaxOptions: { orderId: parentId },
            //height: 500,
            uniqueId: "id",
            pageSize: 10,
            pageList: [10, 25],
            columns: [
                {
                    field: 'orderChannelName',
                    title: '渠道',
                    align: 'center'
                },
                {
                    field: 'orderSku',
                    title: 'SKU',
                    align: 'center'
                },
                {
                    field: 'productName',
                    title: '产品名称',
                    align: 'center'
                },
                {
                    field: 'productColorName',
                    title: '产品颜色',
                    align: 'center'
                },
                {
                    field: 'productCm',
                    title: '尺码(CM)',
                    align: 'center'
                },
                {
                    field: 'productInch',
                    title: '尺码(INCH)',
                    align: 'center'
                },
                {
                    field: 'orderNumber',
                    title: '请购数量',
                    align: 'center'
                },
                {
                    field: 'orderDate',
                    title: '预计交期',
                    align: 'center'
                },
                {
                    field: 'orderProductPrice',
                    title: '参考价',
                    align: 'center'
                },
                {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter2
                }],
            onLoadSuccess : function(){
                $('.edit-btn').on('click',function () {
                    var id = $(this).attr('data-val');
                    $("#id").val(id);
                    var orderNumber = $(this).attr('data-val-number');
                    $("#orderNumber").val(orderNumber);
                    var orderDate = $(this).attr('data-val-date');
                    $("#orderDate").val(orderDate);
                    layer.open({
                        type:1,
                        title:'修改请购单明细',
                        area:['600px','250px'],
                        skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_window')
                    })
                });


                //删除数据字典详情信息
                $('.del-detail-btn').on('click',function(){
                    var delid = $(this).attr('data-val'),delname = $(this).attr('data-val-sku');
                    //删除提示
                    layer.confirm('确定要删除当前数据？<br>['+delname+']', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        $.post('/sea/data/orderDetail/delete.do',{id: delid},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    cur_table.bootstrapTable('refresh');
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,time: 1500});
                            }
                        });
                    });
                });
            }
        });
    }

    function optFormatter2(value,row){
        var content ='';
        if(orderStatus==1) {
            content += '<button class="edit-btn btn btn-xs btn-info" data-val-date="' + row.orderDate + '" data-val-number="' + row.orderNumber + '" data-val="' + row.id + '" title="修改"><i class="fa fa-edit"></i> 修改</button>';
            content += '&nbsp;<a class="del-detail-btn btn btn-xs btn-danger" data-val-sku="' + row.orderSku + '" data-val="' + row.id + '" title="删除"><i class="fa fa-trash-o"></i> 删除 </a>';
        }
        if(orderStatus==2){

        }
        return content;
    }

    $('.search-btn').on('click',function(){
        $relatedTable.bootstrapTable('refresh', { url:'${ctxPath}/sea/data/order/findByCnd.do?'+$('#search-form').serialize() });
    });

    $(".export-btn").click(function () {
        window.location.href='/sea/data/orderDetail/export.do?'+$('#search-form').serialize();
    });

    $(".export-head-btn").click(function () {
        window.location.href='/sea/data/order/export.do?'+$('#search-form').serialize();
    });




    layui.use('upload', function(){
        var upload = layui.upload;
        upload.render({
            elem: '#import-btn', //绑定元素,
            accept:'file',
            url: '/sea/data/order/import/', //上传接口
            data:{"productMember":$("#productMember").val()},
            before:function () {
                layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
            }
            ,done: function(data){
                if( data.ok ){
                    layer.msg(data.msg,{icon: 1,time: 1500},function(){
                        //刷新页面
                        $relatedTable.bootstrapTable('refresh');
                        layer.closeAll();
                    });
                }else{
                    layer.msg(data.msg,{icon: 0,time: 5000},function () {
                        layer.closeAll();
                    });

                }
            }
            ,error: function(){
                //请求异常回调
                layer.closeAll();
            }
        });
    });

    //提交表单
    $('.save-btn').on('click',function(){
        layer.confirm('确定要修改当前数据？', {
            btn: ['确定','取消'],title:'提示'
        }, function(){
            var orderNumber=$("#orderNumber").val();
            if(orderNumber=='' || orderNumber <=0){
                parent.layer.msg('请购数量不能为空并且小于0！',{icon: 0,time:1500,shade: [0.8, '#393D49']});
                return;
            }
            var orderDate=$("#orderDate").val();
            if(orderDate==''){
                parent.layer.msg('请购日期不能为空！',{icon: 0,time:1500,shade: [0.8, '#393D49']});
                return;
            }
            layer.closeAll();
            var index=layer.load();
            $.post('/sea/data/orderDetail/usave.do',{orderNumber:orderNumber,orderDate:orderDate,id:$("#id").val()},function(data){
                if( data.ok ){
                    cur_table.bootstrapTable('refresh');
                    layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }else{
                    layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            });
        });
    });

    //提交表单
    $('.save-check-btn').on('click',function(){
        var orderStatus=$('input:radio:checked').val();
        layer.confirm('确定要审核当前数据？', {
            btn: ['确定','取消'],title:'提示'
        }, function(){
            layer.closeAll();
            var index=layer.load();
            $.post('/sea/data/order/usave.do',{orderCheckMember:$("#productMember").val(),orderRemark:$("#orderRemark").val(),orderStatus:orderStatus,id:$("#order_id").val()},function(data){
                if( data.ok ){
                    $relatedTable.bootstrapTable('refresh');
                    layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }else{
                    layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            });
        });
    });
</script>
</body>
</html>
